package com.fesine.quartz.chapter8;

import com.fesine.quartz.util.FileExtensionFileFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Scheduler;
import org.quartz.SchedulerConfigException;
import org.quartz.SchedulerException;
import org.quartz.simpl.SimpleClassLoadHelper;
import org.quartz.spi.SchedulerPlugin;
import org.quartz.xml.JobSchedulingDataProcessor;

import java.io.File;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/11/20 13:22
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/20 13:22
 */
public class JobLoaderPlugin implements SchedulerPlugin {
    static Log logger = LogFactory.getLog(JobLoaderPlugin.class);
    private String jobsDirectory;
    private File[] jobFiles = null;
    private String pluginName;
    private Scheduler scheduler;
    private boolean validateXML = true;
    private boolean validateSchema = true;

    public JobLoaderPlugin() {

    }

    public File[] getJobFiles() {
        return jobFiles;
    }

    public void setJobFiles(File[] jobFiles) {
        this.jobFiles = jobFiles;
    }

    public String getPluginName() {
        return pluginName;
    }

    public void setPluginName(String pluginName) {
        this.pluginName = pluginName;
    }

    public String getJobsDirectory() {
        return jobsDirectory;
    }

    public void setJobsDirectory(String jobsDirectory) {
        this.jobsDirectory = jobsDirectory;
    }

    public Scheduler getScheduler() {
        return scheduler;
    }

    public void setScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    public boolean isValidateXML() {
        return validateXML;
    }

    public void setValidateXML(boolean validateXML) {
        this.validateXML = validateXML;
    }

    public boolean isValidateSchema() {
        return validateSchema;
    }

    public void setValidateSchema(boolean validateSchema) {
        this.validateSchema = validateSchema;
    }

    @Override
    public void initialize(String name, Scheduler scheduler) throws SchedulerException {
        this.pluginName = name;
        this.scheduler = scheduler;
        logger.info("注册插件：" + pluginName);
        loadJobs();
    }

    private void loadJobs() throws SchedulerException {
        File dir = null;
        if (getJobsDirectory() == null || !(dir = new File(getJobsDirectory())).exists()) {
            throw new SchedulerConfigException("任务目录不存在！" + jobsDirectory);
        }
        logger.info("加载job任务！" + dir.getName());
        //this.jobFiles = dir.listFiles(new XMLFileOnlyFilter());
        this.jobFiles = dir.listFiles(new FileExtensionFileFilter(".xml"));

    }

    @Override
    public void start() {
        processJobs();
    }

    private void processJobs() {
        if (getJobFiles() == null || getJobFiles().length == 0) {
            return;
        }

        JobSchedulingDataProcessor processor = new JobSchedulingDataProcessor(new
                SimpleClassLoadHelper(), isValidateXML
                (), isValidateSchema());
        int size = getJobFiles().length;
        for (int i = 0; i < size; i++) {

            File job = getJobFiles()[i];
            String fileName = job.getAbsolutePath();
            logger.debug("加载文件:" + fileName);
            try {
                processor.processFileAndScheduleJobs(fileName,scheduler,true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void shutdown() {

    }
}
